package com.zivwu.mvp.view;

import com.zivwu.mvp.presenter.IPresenter;
import com.zivwu.mvp.view.load.LoadingController;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.disposables.CompositeDisposable;

/**
 * Author by ziv, Email wuming5445@dingtalk.com, Date on 2019-11-05.
 */
public abstract class BaseView implements IView {

    private CompositeDisposable mDisposable;
    private LoadingController mLoadingController;
    private List<IPresenter.Presenter> presenters;


    public BaseView() {
        init();
    }

    private void init() {
        presenters = new ArrayList<>();
        mDisposable = new CompositeDisposable();
        mLoadingController = new LoadingController(this);
    }

    public void onStart() {
        for (IPresenter.Presenter presenter : presenters)
            presenter.onStart();
    }

    @Override
    public LoadingController getLoadingController() {
        return mLoadingController;
    }

    @Override
    public CompositeDisposable getDisposable() {
        return mDisposable;
    }


    public void onDestroy() {
        for (IPresenter.Presenter presenter : presenters) {
            presenter.onDestroy();
        }
        presenters.clear();
        mDisposable.dispose();
        mLoadingController.onDestroy();
        presenters = null;
        mDisposable = null;
    }
}
